using System;
using System.Configuration;
using System.Data;
using BizElements.Core;

namespace BizElements.BusinessLayer
{
    /// <summary>
    /// Entity model class for the table/view 'TextSource'.
    /// </summary>
    [Serializable]
    class TextSourceEntity : EntityModelBase
    {
        #region Private members.

        // Members mapped to database columns.
        private int? id;
        private string application;
        private string module;
        private string name;
        private string languageCode;
        private string cultureCode;
        private string value;
        private string description;
        private byte? status;


        #endregion

        #region Protected properties.

        // Properties mapped to database columns.    
        protected int? _Id
        {
            get
            {
                return this.id;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.id.HasValue) || (value.HasValue && (value.Value != this.id.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.id = value;
            }
        }

        protected string _Application
        {
            get
            {
                return this.application;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.application)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.application = value;
            }
        }

        protected string _Module
        {
            get
            {
                return this.module;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.module)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.module = value;
            }
        }

        protected string _Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.name)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.name = value;
            }
        }

        protected string _LanguageCode
        {
            get
            {
                return this.languageCode;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.languageCode)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.languageCode = value;
            }
        }

        protected string _CultureCode
        {
            get
            {
                return this.cultureCode;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.cultureCode)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.cultureCode = value;
            }
        }

        protected string _Value
        {
            get
            {
                return this.value;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.value)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.value = value;
            }
        }

        protected string _Description
        {
            get
            {
                return this.description;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.description)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.description = value;
            }
        }

        protected byte? _Status
        {
            get
            {
                return this.status;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.status.HasValue) || (value.HasValue && (value.Value != this.status.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.status = value;
            }
        }


        #endregion

        #region Constructors.

        /// <summary>
        /// Default constructor.
        /// </summary>
        public TextSourceEntity()
            : base(new TextSourceMeta())
        {
        }

        /// <summary>
        /// Constructor. Initalizes Table property with the specified (shared) instance.
        /// </summary>
        /// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
        public TextSourceEntity(TextSourceMeta dbTable)
            : base(dbTable)
        {
        }

        /// <summary>
        /// Constructor. Initializes primary key. Useful for methods that don't require other values (eg. DeleteOne, SelectOne).
        /// </summary>
        public TextSourceEntity(int id)
            : base(new TextSourceMeta())
        {
            _Id = id;
        }

        /// <summary>
        /// Constructor. Initializes primary key. Useful for methods that don't require other values (eg. DeleteOne, SelectOne).
        /// </summary>
        public TextSourceEntity(TextSourceMeta dbTable, int id)
            : base(dbTable)
        {
            _Id = id;
        }

        /// <summary>
        /// Constructor. Initializes members with the values in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public TextSourceEntity(DataRow row)
            : base(new TextSourceMeta())
        {
            FromDataRow(row);
        }

        /// <summary>
        /// Constructor. Initializes members with the values in the given DataRow.
        /// </summary>
        /// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public TextSourceEntity(TextSourceMeta dbTable, DataRow row)
            : base(dbTable)
        {
            FromDataRow(row);
        }

        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="existing">Instance of TextSourceEntity.</param>
        public TextSourceEntity(TextSourceEntity existing)
            : base(existing.Table)
        {
            FromExistingEntity(existing);
        }

        static readonly Type DbTableClass = typeof(TextSourceMeta);

        /// <summary>
        /// Initializes all members of TextSourceEntity and base EntityModelBase class. This is the fastest way to initialize entity with data from database.
        /// </summary>
        private TextSourceEntity(IDbTable table, EntityState entityState, int? id, string application, string module, string name, string languageCode, string cultureCode, string value, string description, byte? status)
            : base(table, DbTableClass, entityState)
        {
            this.id = id;
            this.application = application;
            this.module = module;
            this.name = name;
            this.languageCode = languageCode;
            this.cultureCode = cultureCode;
            this.value = value;
            this.description = description;
            this.status = status;
        }

        /// <summary>
        /// Initializes all members mapped to fields.
        /// </summary>
        protected void Init(int? id, string application, string module, string name, string languageCode, string cultureCode, string value, string description, byte? status)
        {
            _Id = id;
            _Application = application;
            _Module = module;
            _Name = name;
            _LanguageCode = languageCode;
            _CultureCode = cultureCode;
            _Value = value;
            _Description = description;
            _Status = status;
        }

        #endregion

        #region Methods.

        /// <summary>
        /// Gets the value(s) that uniquely identifiy an entity.
        /// In the order as specified in accompanying IDbTable metadata class.
        /// NULL if the parent table/view doesn't have a primary key constraint or the required fields are not set.
        /// </summary>
        public override object[] GetPrimaryKeyValue()
        {
            if (_Id != null)
                return new object[] { _Id.Value };
            else
                return null;
        }

        /// <summary>
        /// Initializes entity members with data stored in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with the required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public override void FromDataRow(DataRow row)
        {
            object currentColumnValue;

            currentColumnValue = TryGetColumnValue(row, "Id");
            _Id = (currentColumnValue != DBNull.Value) ? (int?)Convert.ToInt32(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Application");
            _Application = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Module");
            _Module = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Name");
            _Name = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "LanguageCode");
            _LanguageCode = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "CultureCode");
            _CultureCode = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Value");
            _Value = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Description");
            _Description = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Status");
            _Status = (currentColumnValue != DBNull.Value) ? (byte?)Convert.ToByte(currentColumnValue) : null;

            bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
            if (setEntityStateToSynchronized)
                this.EntityState = EntityState.Synchronized;
        }

        private static object TryGetColumnValue(DataRow row, string columnName)
        {
            int colIdx = row.Table.Columns.IndexOf(columnName);
            return (colIdx >= 0) ? row[colIdx] : null;
        }

        private static object GetColumnValue(DataRow row, IDbColumn column)
        {
            int colIdx = row.Table.Columns.IndexOf(column.Alias);
            if (colIdx < 0)
                colIdx = row.Table.Columns.IndexOf(column.ColumnName);
            if (colIdx < 0)
                throw new ArgumentException("DataTable doesn't contain the specified column (" + column.ColumnName + ").");

            return row[colIdx];
        }

        /// <summary>
        /// Initializes entity members with data stored in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with all or some of the columns defined in meta data.</param>
        /// <param name="fieldMetaData"><see cref="IDbTable"/> meta data object which links ADO.NET row columns to entity properties.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public override void FromDataRow(DataRow row, IDbTable fieldMetaData)
        {
            IDbColumn currentColumn;
            object currentColumnValue;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Id.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Id = (currentColumnValue != DBNull.Value) ? (int?)Convert.ToInt32(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Application.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Application = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Module.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Module = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Name.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Name = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.LanguageCode.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _LanguageCode = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.CultureCode.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _CultureCode = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Value.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Value = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Description.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Description = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Status.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Status = (currentColumnValue != DBNull.Value) ? (byte?)Convert.ToByte(currentColumnValue) : null;

            bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
            if (setEntityStateToSynchronized)
                this.EntityState = EntityState.Synchronized;
        }

        /// <summary>
        /// Converts the given DataRow to TextSourceEntity.
        /// </summary>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public static explicit operator TextSourceEntity(DataRow row)
        {
            return new TextSourceEntity(row);
        }

        /// <summary>
        /// Creates an array of objects containing entity data.
        /// </summary>
        /// <returns>Entity values.</returns>
        public override object[] ToObjectArray()
        {
            object[] values = new object[9];

            values[0] = (_Id != null) ? (object)_Id.Value : null;
            values[1] = (_Application != null) ? (object)_Application : null;
            values[2] = (_Module != null) ? (object)_Module : null;
            values[3] = (_Name != null) ? (object)_Name : null;
            values[4] = (_LanguageCode != null) ? (object)_LanguageCode : null;
            values[5] = (_CultureCode != null) ? (object)_CultureCode : null;
            values[6] = (_Value != null) ? (object)_Value : null;
            values[7] = (_Description != null) ? (object)_Description : null;
            values[8] = (_Status != null) ? (object)_Status.Value : null;

            return values;
        }

        /// <summary>
        /// Initializes entity members with the given values.
        /// </summary>
        /// <param name="entityValues">Array with the required values.</param>
        public override void FromObjectArray(object[] entityValues)
        {
            _Id = (entityValues[0] != null) ? (int?)Convert.ToInt32(entityValues[0]) : null;
            _Application = (entityValues[1] != null) ? (string)Convert.ToString(entityValues[1]) : null;
            _Module = (entityValues[2] != null) ? (string)Convert.ToString(entityValues[2]) : null;
            _Name = (entityValues[3] != null) ? (string)Convert.ToString(entityValues[3]) : null;
            _LanguageCode = (entityValues[4] != null) ? (string)Convert.ToString(entityValues[4]) : null;
            _CultureCode = (entityValues[5] != null) ? (string)Convert.ToString(entityValues[5]) : null;
            _Value = (entityValues[6] != null) ? (string)Convert.ToString(entityValues[6]) : null;
            _Description = (entityValues[7] != null) ? (string)Convert.ToString(entityValues[7]) : null;
            _Status = (entityValues[8] != null) ? (byte?)Convert.ToByte(entityValues[8]) : null;
        }

        /// <summary>
        /// Gets the parent entity defined by the given foreign key.
        /// </summary>
        /// <param name="foreignKey">FK which must be defined in the TextSourceMeta class or an exception is generated.</param>
        /// <returns>Parent entity. NULL if the FK fields haven't been set or if the entity with the given key values doesn't exist.</returns>
        public override IEntity GetParent(DbRelation foreignKey)
        {
            throw new Exception("No foreign keys are defined in the TextSourceMeta class.");
        }

        /// <summary>
        /// Sets the given value into the member that represents the parent entity defined by the foreign key.
        /// </summary>
        /// <param name="foreignKey">FK which must be defined in the CFunctionsMeta class or an exception is generated.</param>
        /// <param name="entity">Parent entity. May be NULL. Must be an instance of the CFunctionsEntity or a derived class.</param>
        public override void SetParent(DbRelation foreignKey, IEntity entity)
        {
            throw new Exception("No foreign keys are defined in the TextSourceMeta class.");
        }

        #endregion

        /// <summary>
        /// Gets typed IDbTable object for the entity's table/view.
        /// </summary>
        public TextSourceMeta Table
        {
            get { return (TextSourceMeta)_Table; }
        }

        #region Public properties mapped to database columns.

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Id'.
        /// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
        /// </summary>
        public virtual int Id
        {
            get
            {
                if (_Id == null)
                {
                    if (this.NullCheckEnabled)
                        throw new NoNullAllowedException("TextSourceEntity.get_Id: Id is not set yet.");
                    else
                        return default(int);
                }

                return _Id.Value;
            }
            set
            {
                _Id = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Application'.
        /// </summary>
        public virtual string Application
        {
            get
            {
                return _Application;
            }
            set
            {
                _Application = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Module'.
        /// </summary>
        public virtual string Module
        {
            get
            {
                return _Module;
            }
            set
            {
                _Module = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Name'.
        /// </summary>
        public virtual string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'LanguageCode'.
        /// </summary>
        public virtual string LanguageCode
        {
            get
            {
                return _LanguageCode;
            }
            set
            {
                _LanguageCode = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'CultureCode'.
        /// </summary>
        public virtual string CultureCode
        {
            get
            {
                return _CultureCode;
            }
            set
            {
                _CultureCode = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'Value'.
        /// </summary>
        public virtual string Value
        {
            get
            {
                return _Value;
            }
            set
            {
                _Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'Description'.
        /// </summary>
        public virtual string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Status'.
        /// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
        /// </summary>
        public virtual byte Status
        {
            get
            {
                if (_Status == null)
                {
                    if (this.NullCheckEnabled)
                        throw new NoNullAllowedException("TextSourceEntity.get_Status: Status is not set yet.");
                    else
                        return default(byte);
                }

                return _Status.Value;
            }
            set
            {
                _Status = value;
            }
        }

        #endregion

        #region NewEntity static methods suitable for usage in EntityBuilder<T>.

        /// <summary>Creates new entity and initializes all members of with the given values.</summary>
        /// <param name="table">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
        /// <param name="values">Array which contains values for all properties mapped to database columns in the following order: Id, Application, Module, Name, LanguageCode, CultureCode, Value, Description, Status.</param>
        /// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
        /// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
        /// with IDataConverter objects which retrieve property values directly from IDataReader objects.</remarks>
        public static TextSourceEntity NewEntity(IDbTable table, EntityState entityState, object[] values)
        {
            return new TextSourceEntity(table, entityState, (int)values[0], (string)values[1], (string)values[2], (string)values[3], (string)values[4], (string)values[5], (string)ReplaceDbNull(values[6]), (string)ReplaceDbNull(values[7]), (byte)values[8]);
        }

        /// <summary>Creates new entity and initializes members for which values are defined in the array.</summary>
        /// <param name="table">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
        /// <param name="values">Array which contains values or nulls for all properties mapped to database columns in the following order: Id, Application, Module, Name, LanguageCode, CultureCode, Value, Description, Status.</param>
        /// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
        /// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
        /// with IObjectReader objects which retrieve property values directly from IDataReader objects.</remarks>
        public static TextSourceEntity NewPartialEntity(IDbTable table, EntityState entityState, object[] values)
        {
            return new TextSourceEntity(table, entityState, CastTo<int>(values[0]), (string)ReplaceDbNull(values[1]), (string)ReplaceDbNull(values[2]), (string)ReplaceDbNull(values[3]), (string)ReplaceDbNull(values[4]), (string)ReplaceDbNull(values[5]), (string)ReplaceDbNull(values[6]), (string)ReplaceDbNull(values[7]), CastTo<byte>(values[8]));
        }

        private static T? CastTo<T>(object val)
            where T : struct
        {
            if (IsNull(val))
                return null;
            else
                return (T)val;
        }

        private static bool IsNull(object val)
        {
            return (val == DBNull.Value || val == null);
        }

        private static object ReplaceDbNull(object dbValue)
        {
            return (dbValue != DBNull.Value) ? dbValue : null;
        }

        #endregion
    }

    /// <summary>
    /// Typed IDbTable class for the table/view 'TextSource'.
    /// </summary>
    [Serializable]
    sealed class TextSourceMeta : SealedDbTable
    {
        static TextSourceMeta()
        {
            ReadSequenceOverrideFromConfig();
            ReadColumnNamesFromConfig();
            ImmutableColumnProperties = CreateImmutableColumnProperties();
            ImmutableTableProperties = CreateImmutableTableProperties();
        }

        #region Configuration.

        static readonly string[] SequenceNameOverrides = new string[9];
        static readonly bool?[] AutoIncrementOverrides = new bool?[9];

        private static void ReadSequenceOverrideFromConfig()
        {
            SequenceNameOverrides[0] = ConfigurationManager.AppSettings["TextSource.Id.SequenceName"];
            AutoIncrementOverrides[0] = (SequenceNameOverrides[0] != null) ? (bool?)true : null;
            SequenceNameOverrides[1] = ConfigurationManager.AppSettings["TextSource.Application.SequenceName"];
            AutoIncrementOverrides[1] = (SequenceNameOverrides[1] != null) ? (bool?)true : null;
            SequenceNameOverrides[2] = ConfigurationManager.AppSettings["TextSource.Module.SequenceName"];
            AutoIncrementOverrides[2] = (SequenceNameOverrides[2] != null) ? (bool?)true : null;
            SequenceNameOverrides[3] = ConfigurationManager.AppSettings["TextSource.Name.SequenceName"];
            AutoIncrementOverrides[3] = (SequenceNameOverrides[3] != null) ? (bool?)true : null;
            SequenceNameOverrides[4] = ConfigurationManager.AppSettings["TextSource.LanguageCode.SequenceName"];
            AutoIncrementOverrides[4] = (SequenceNameOverrides[4] != null) ? (bool?)true : null;
            SequenceNameOverrides[5] = ConfigurationManager.AppSettings["TextSource.CultureCode.SequenceName"];
            AutoIncrementOverrides[5] = (SequenceNameOverrides[5] != null) ? (bool?)true : null;
            SequenceNameOverrides[6] = ConfigurationManager.AppSettings["TextSource.Value.SequenceName"];
            AutoIncrementOverrides[6] = (SequenceNameOverrides[6] != null) ? (bool?)true : null;
            SequenceNameOverrides[7] = ConfigurationManager.AppSettings["TextSource.Description.SequenceName"];
            AutoIncrementOverrides[7] = (SequenceNameOverrides[7] != null) ? (bool?)true : null;
            SequenceNameOverrides[8] = ConfigurationManager.AppSettings["TextSource.Status.SequenceName"];
            AutoIncrementOverrides[8] = (SequenceNameOverrides[8] != null) ? (bool?)true : null;
        }

        static readonly string[] ColumnNames = new string[9];

        private static void ReadColumnNamesFromConfig()
        {
            ColumnNames[0] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Id") ?? "Id";
            ColumnNames[1] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Application") ?? "Application";
            ColumnNames[2] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Module") ?? "Module";
            ColumnNames[3] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Name") ?? "Name";
            ColumnNames[4] = TextSourceCatalog.GetColumnNameOverride("TextSource", "LanguageCode") ?? "LanguageCode";
            ColumnNames[5] = TextSourceCatalog.GetColumnNameOverride("TextSource", "CultureCode") ?? "CultureCode";
            ColumnNames[6] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Value") ?? "Value";
            ColumnNames[7] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Description") ?? "Description";
            ColumnNames[8] = TextSourceCatalog.GetColumnNameOverride("TextSource", "Status") ?? "Status";
        }

        #endregion

        #region Singleton/immutable configuration objects.

        static readonly DbColumnConfiguration[] ImmutableColumnProperties;
        static readonly DbTableConfiguration ImmutableTableProperties;

        private static DbColumnConfiguration[] CreateImmutableColumnProperties()
        {
            return new DbColumnConfiguration[]
			{
				new DbColumnConfiguration(ColumnNames[0], DbType.Int32, typeof(int), false, 0, true, null, 0, true, false, false, "Id", int.MinValue, int.MaxValue, false, SequenceNameOverrides[0]),
				new DbColumnConfiguration(ColumnNames[1], DbType.AnsiString, typeof(string), false, 1, AutoIncrementOverrides[1] ?? false, null, 255, false, false, false, "Application", null, null, false, SequenceNameOverrides[1]),
				new DbColumnConfiguration(ColumnNames[2], DbType.AnsiString, typeof(string), false, 2, AutoIncrementOverrides[2] ?? false, null, 255, false, false, false, "Module", null, null, false, SequenceNameOverrides[2]),
				new DbColumnConfiguration(ColumnNames[3], DbType.AnsiString, typeof(string), false, 3, AutoIncrementOverrides[3] ?? false, null, 255, false, false, false, "Name", null, null, false, SequenceNameOverrides[3]),
				new DbColumnConfiguration(ColumnNames[4], DbType.AnsiStringFixedLength, typeof(string), false, 4, AutoIncrementOverrides[4] ?? false, null, 2, false, false, false, "LanguageCode", null, null, false, SequenceNameOverrides[4]),
				new DbColumnConfiguration(ColumnNames[5], DbType.AnsiString, typeof(string), false, 5, AutoIncrementOverrides[5] ?? false, null, 255, false, false, false, "CultureCode", null, null, false, SequenceNameOverrides[5]),
				new DbColumnConfiguration(ColumnNames[6], DbType.String, typeof(string), true, 6, AutoIncrementOverrides[6] ?? false, null, 2000, false, false, false, "Value", null, null, false, SequenceNameOverrides[6]),
				new DbColumnConfiguration(ColumnNames[7], DbType.String, typeof(string), true, 7, AutoIncrementOverrides[7] ?? false, null, 2000, false, false, false, "Description", null, null, false, SequenceNameOverrides[7]),
				new DbColumnConfiguration(ColumnNames[8], DbType.Byte, typeof(byte), false, 8, AutoIncrementOverrides[8] ?? false, null, 0, false, false, false, "Status", byte.MinValue, byte.MaxValue, false, SequenceNameOverrides[8]),
			};
        }

        private static DbTableConfiguration CreateImmutableTableProperties()
        {
            return new DbTableConfiguration
            (
                TextSourceCatalog.GetTableNameOverride("TextSource") ?? "TextSource",
                new TextSourceCatalog(),
                ImmutableColumnProperties,
                new int[] { 0 },
                new string[] { },
                /*is view*/ false
            );
        }

        #endregion

        #region Constructors.

        /// <summary>
        /// Initializes a new instance of TextSourceMeta class.
        /// </summary>
        public TextSourceMeta()
            : base(ImmutableTableProperties, null, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of TextSourceMeta class.
        /// </summary>
        /// <param name="alias">Object alias. If NULL then it will be equal to the table name.</param>
        public TextSourceMeta(string alias)
            : base(ImmutableTableProperties, alias, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of TextSourceMeta class.
        /// </summary>
        /// <param name="setPrefixedColumnAliases">Whether to change aliases of all columns so that they start with prefix (usually table name).</param>
        public TextSourceMeta(bool setPrefixedColumnAliases)
            : base(ImmutableTableProperties, null, setPrefixedColumnAliases)
        {
        }

        /// <summary>
        /// Initializes a new instance of TextSourceMeta class.
        /// </summary>
        /// <param name="alias">Object alias. If NULL then it will be equal to the table name.</param>
        /// <param name="setPrefixedColumnAliases">Whether to change aliases of all columns so that they start with prefix (usually table name).</param>
        public TextSourceMeta(string alias, bool setPrefixedColumnAliases)
            : base(ImmutableTableProperties, alias, setPrefixedColumnAliases)
        {
        }

        #endregion

        #region CreateForeignKeys.

        /// <summary>
        /// Initializes an array of relations used by foreign keys.
        /// Only relations to other generated IDbTable classes are created.
        /// </summary>
        protected override DbRelation[] CreateForeignKeys()
        {
            DbRelation[] foreignKeys = new DbRelation[0];

            return foreignKeys;
        }

        #endregion

        #region New* methods.

        /// <summary>
        /// Creates a new TextSourceEntity object.
        /// </summary>
        /// <returns>New entity.</returns>
        public override IEntity NewEntity()
        {
            return new TextSourceEntity(this);
        }

        /// <summary>
        /// Creates a new EntityReader&lt;TextSourceEntity&gt; object.
        /// </summary>
        /// <returns>An instance of EntityReader&lt;TextSourceEntity&gt; class.</returns>
        public override IObjectReader NewEntityReader()
        {
            // Use default EntityReader<T>() constructor when targeting SQLite database or when column types are compatible/convertible, but do not exactly match, entity property types.
            return new EntityReader<TextSourceEntity>(TextSourceEntity.NewEntity);
        }

        #endregion

        #region Clone.

        /// <summary>
        /// Creates another IDbTable object for the same table/view.
        /// </summary>
        /// <param name="cloneAlias">Clone alias.</param>
        /// <returns>Clone.</returns>
        public override IDbTable Clone(string cloneAlias)
        {
            return new TextSourceMeta(cloneAlias);
        }

        /// <summary>
        /// Creates another IDbTable object for the same table/view.
        /// </summary>
        /// <param name="cloneAlias">Clone alias.</param>
        /// <param name="setPrefixedAliases">Specifies whether cloned columns will have prefixed aliases.</param>
        /// <returns>Clone.</returns>
        public override IDbTable Clone(string cloneAlias, bool setPrefixedAliases)
        {
            return new TextSourceMeta(cloneAlias, setPrefixedAliases);
        }

        #endregion

        #region Columns.

        /// <summary>
        /// Gets metadata for 'Id' column.
        /// </summary>
        public IDbColumn Id
        {
            get { return this.Columns[0]; }
        }

        /// <summary>
        /// Gets metadata for 'Application' column.
        /// </summary>
        public IDbColumn Application
        {
            get { return this.Columns[1]; }
        }

        /// <summary>
        /// Gets metadata for 'Module' column.
        /// </summary>
        public IDbColumn Module
        {
            get { return this.Columns[2]; }
        }

        /// <summary>
        /// Gets metadata for 'Name' column.
        /// </summary>
        public IDbColumn Name
        {
            get { return this.Columns[3]; }
        }

        /// <summary>
        /// Gets metadata for 'LanguageCode' column.
        /// </summary>
        public IDbColumn LanguageCode
        {
            get { return this.Columns[4]; }
        }

        /// <summary>
        /// Gets metadata for 'CultureCode' column.
        /// </summary>
        public IDbColumn CultureCode
        {
            get { return this.Columns[5]; }
        }

        /// <summary>
        /// Gets metadata for 'Value' column.
        /// </summary>
        public IDbColumn Value
        {
            get { return this.Columns[6]; }
        }

        /// <summary>
        /// Gets metadata for 'Description' column.
        /// </summary>
        public IDbColumn Description
        {
            get { return this.Columns[7]; }
        }

        /// <summary>
        /// Gets metadata for 'Status' column.
        /// </summary>
        public IDbColumn Status
        {
            get { return this.Columns[8]; }
        }

        #endregion

        #region Foreign keys.

        #endregion

        #region Parent entity property name getters.

        #endregion

        #region Children.

        /// <summary>
        /// Gets tables which reference the current table.
        /// </summary>
        /// <returns>Array of tables or empty array if the current table is not referenced by other objects.</returns>
        public override IDbTable[] GetChildTables()
        {
            return new IDbTable[]
			{
			};
        }

        /// <summary>
        /// Gets relations where current table acts as a parent.
        /// </summary>
        /// <returns>Array of relations or empty array if the current table is not referenced by other objects.</returns>
        public override DbRelation[] GetChildRelations()
        {
            return new DbRelation[]
			{
			};
        }

        #endregion
    }
}
